/**
 * Project Name:jh
 * File Name:IPCamRadarSrvInfoImpl.java
 * Package Name:com.jh.service
 * Date:下午5:12:31
 * Copyright (c) 2021, LZ All Rights Reserved.
 *
*/

package com.jh.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jh.dao.IPCamRadarInfoDAO;
import com.jh.dao.impl.IPCamRadarInfoDAOImpl;
import com.jh.pojo.IPCamRadarInfo;
import com.jh.service.IPCamRadarInfoSrv;
import com.jh.utils.DBUtils;

/**
 * Description: <br/>
 * Date: 下午5:12:31 <br/>
 * 
 * @author lz E-mail:withitren@outlook.com
 * @version
 * @see
 */

public class IPCamRadarInfoSrvImpl implements IPCamRadarInfoSrv {
    static final Logger logger = LoggerFactory.getLogger(IPCamRadarInfoSrvImpl.class);

    private IPCamRadarInfoDAO dao = new IPCamRadarInfoDAOImpl();

    /*
     * ascOrDesc:true->升序， false->降序
     * 
     */
    @Override
    public List<IPCamRadarInfo> list(IPCamRadarInfo iri, boolean ascOrDesc) {

        try {
            StringBuilder sql = new StringBuilder("select * from IPCamRadarInfo where sp_mark=1 ");
            List<Object> params = new ArrayList<Object>();
            if (null != iri) {
                // if(iri.getSp_name() != null && !"".equals(iri.getSp_name())){
                if (iri.getSp_name() != null && iri.getSp_name().length() != 0) {
                    sql.append("and sp_name like ? ");
                    params.add("%" + iri.getSp_name() + "%");
                }
                if (iri.getSp_id() != null && (iri.getSp_id() != 0)) {
                    sql.append("and sp_id like ? ");
                    params.add(iri.getSp_id());
                }

            }
            if (true == ascOrDesc) {
                sql.append("order by sp_id asc ");
            } else {
                sql.append("order by sp_id desc ");
            }

            logger.debug("--->" + sql.toString() + params.toString());
            return dao.list(sql.toString(), params.toArray());
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

    @Override
    public boolean add(IPCamRadarInfo iri) {

        try {
            String sql = "insert into IPCamRadarInfo (sp_name, sp_desc,radar_ip,radar_port,radar_status,ipcam_ip,ipcam_name,ipcam_pwd,ipcam_url,ipcam_status,sp_mark) values(?,?,?,?,?,?,?,?,?,?,?) ";
            List<Object> list = new ArrayList<Object>();
            if (null != iri) {

                // if (iri.getSp_name() != null && "".equals(iri.getSp_name()))
                // {
                if (iri.getSp_name() != null && iri.getSp_name().length() != 0) {
                    list.add(iri.getSp_name());
                } else {
                    list.add("无设备");
                }

                if (iri.getSp_desc() != null && iri.getSp_desc().length() != 0) {
                    list.add(iri.getSp_desc());
                } else {
                    list.add("...");
                }

                if (iri.getRadar_ip() != null && iri.getRadar_ip().length() != 0) {
                    list.add(iri.getRadar_ip());
                } else {
                    list.add("无设备");
                }

                if (iri.getRadar_port() != null && iri.getRadar_port().length() != 0) {
                    list.add(iri.getRadar_port());
                } else {
                    list.add("无设备");
                }

                if (iri.getRadar_status() != null && iri.getRadar_status().length() != 0) {
                    list.add(iri.getRadar_status());
                } else {
                    list.add("离线");
                }

                if (iri.getIpcam_ip() != null && iri.getIpcam_ip().length() != 0) {
                    list.add(iri.getIpcam_ip());
                } else {
                    list.add("无设备");
                }

                if (iri.getIpcam_name() != null && iri.getIpcam_name().length() != 0) {
                    list.add(iri.getIpcam_name());
                } else {
                    list.add("admin");
                }

                if (iri.getIpcam_pwd() != null && iri.getIpcam_pwd().length() != 0) {
                    list.add(iri.getIpcam_pwd());
                } else {
                    list.add("admin1234");
                }

                if (iri.getIpcam_url() != null && iri.getIpcam_url().length() != 0) {
                    list.add(iri.getIpcam_url());
                } else {
                    list.add("离线");
                }

                if (iri.getIpcam_status() != null && iri.getIpcam_status().length() != 0) {
                    list.add(iri.getIpcam_status());
                } else {
                    list.add("离线");
                }

                /*sp_mark:1 添加，0 删除*/
                list.add("1");
                System.out.println(sql + " ----  " + list.toString());
                logger.debug("--->" + sql + list.toString());
                return DBUtils.addDelUpdate(sql, list.toArray());
            }

        } catch (

        Exception e) {
            logger.error("--->" + e.toString());
        }
        return false;
    }

    @Override
    public boolean delete(IPCamRadarInfo iri) {

        try {
            String sql = "update IPCamRadarInfo set sp_mark=? where sp_id=? ";
            List<Object> list = new ArrayList<Object>();
            if (null != iri) {
                list.add(0);
                list.add(iri.getSp_id());
            }
            logger.debug("--->" + sql + list.toString());
            return DBUtils.addDelUpdate(sql, list.toArray());

        } catch (Exception e) {
            logger.error("--->", e.toString());
        }
        return false;
    }

    @Override
    public boolean update(IPCamRadarInfo iri) {

        try {
            String sql = "update IPCamRadarInfo set sp_name=?, sp_desc=?,radar_ip=?, radar_port=?, radar_status=?,ipcam_ip=?,ipcam_name=?,ipcam_pwd=?,ipcam_url=?,ipcam_status=?, sp_mark=? where sp_id=? ";
            List<Object> list = new ArrayList<Object>();
            if (null != iri) {
                if (iri.getSp_name() != null && iri.getSp_name().length() != 0) {
                    list.add(iri.getSp_name());
                } else {
                    list.add("无设备");
                }

                if (iri.getSp_desc() != null && iri.getSp_desc().length() != 0) {
                    list.add(iri.getSp_desc());
                } else {
                    list.add("...");
                }

                if (iri.getRadar_ip() != null && iri.getRadar_ip().length() != 0) {
                    list.add(iri.getRadar_ip());
                } else {
                    list.add("无设备");
                }

                if (iri.getRadar_port() != null && iri.getRadar_port().length() != 0) {
                    list.add(iri.getRadar_port());
                } else {
                    list.add("无设备");
                }

                if (iri.getRadar_status() != null && iri.getRadar_status().length() != 0) {
                    list.add(iri.getRadar_status());
                } else {
                    list.add("离线");
                }

                if (iri.getIpcam_ip() != null && iri.getIpcam_ip().length() != 0) {
                    list.add(iri.getIpcam_ip());
                } else {
                    list.add("无设备");
                }

                if (iri.getIpcam_name() != null && iri.getIpcam_name().length() != 0) {
                    list.add(iri.getIpcam_name());
                } else {
                    list.add("admin");
                }

                if (iri.getIpcam_pwd() != null && iri.getIpcam_pwd().length() != 0) {
                    list.add(iri.getIpcam_pwd());
                } else {
                    list.add("admin1234");
                }

                if (iri.getIpcam_url() != null && iri.getIpcam_url().length() != 0) {
                    list.add(iri.getIpcam_url());
                } else {
                    list.add("离线");
                }

                if (iri.getIpcam_status() != null && iri.getIpcam_status().length() != 0) {
                    list.add(iri.getIpcam_status());
                } else {
                    list.add("离线");
                }

                /*sp_mark:1 添加，0 删除*/
                list.add("1");
                list.add(iri.getSp_id());
            }
            logger.debug("--->" + sql + list.toString());
            System.out.println("--->" + sql + list.toString());
            return DBUtils.addDelUpdate(sql, list.toArray());

        } catch (Exception e) {
            logger.error("--->", e.toString());
        }
        return false;
    }

    @Override
    public IPCamRadarInfo getIPCamRadarInfoBySpID(IPCamRadarInfo iri) {

        try {
            String sql = "select * from IPCamRadarInfo where sp_mark=1 and sp_id=? ";
            return dao.getIpCamRadarInfoBySpID(sql, iri);
        } catch (Exception e) {
            logger.error("--->", e.toString());
        }
        return null;
    }

    @Override
    public IPCamRadarInfo getIPCamRadarInfoBySpName(IPCamRadarInfo iri) {

        try {
            String sql = "select * from IPCamRadarInfo where sp_mark=1 and sp_name=? ";
            return dao.getIpCamRadarInfoBySpName(sql, iri);
        } catch (Exception e) {
            logger.error("--->", e.toString());
        }
        return null;
    }

    @Override
    public IPCamRadarInfo getIPCamRadarInfoByRadarIP(IPCamRadarInfo iri) {

        try {
            String sql = "select * from IPCamRadarInfo where sp_mark=1 and radar_ip=? ";
            return dao.getIpCamRadarInfoByRadarIP(sql, iri);
        } catch (Exception e) {
            logger.error("--->", e.toString());
        }
        return null;
    }

    @Override
    public IPCamRadarInfo getIPCamRadarInfoByIPcamIP(IPCamRadarInfo iri) {

        try {
            String sql = "select * from IPCamRadarInfo where sp_mark=1 and ipcam_ip=? ";
            return dao.getIpCamRadarInfoByIPCamIP(sql, iri);
        } catch (Exception e) {
            logger.error("--->", e.toString());
        }
        return null;
    }

}
